﻿using System;
using System.Net;
using SCG.General;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //DoppleShips
    public class DoppleShip : WarShip
    {
        //Constructor
        public DoppleShip(SVGame game, Player owner, int engines, int weapons, StarSystem origin)
            : base(game, owner, engines, weapons, origin)
        {
            //assign an approproate Dopple value
            do
            {
                _doppleMult = SVGame.RNG.NextDouble();
            }
            while (_doppleMult > 0.5d && _doppleMult < 0.1d);
        }

        //Ship name
        public override string Name
        {
            get
            {
                if (OwnedByCurrentPlayer)
                    return "DoppleShip";
                else
                    return "WarShip";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "DoppleShips function exactly like WarShips, except to the enemy they appear as WarShips with much lower Engine and Weapon ratings than they actually have.";
            }
        }

        //Ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.DoppleShip;
            }
        }
     
        //Dopple Engines
        public int DoppleEngines
        {
            get
            {
                return GetDoppleValue(Engines);
            }
        }

        //Dopple MaxEngines
        public int DoppleMaxEngines
        {
            get
            {
                return GetDoppleValue(MaxEngines);
            }
        }

        //Dopple Value
        public int DoppleValue
        {
            get
            {
                return GetDoppleValue(EffectiveWeapons);
            }
        }

        //Dopple max value
        public int DoppleMaxValue
        {
            get
            {
                return GetDoppleValue(MaxValue);
            }
        }

        //Dopple known weapons
        public int GetDoppleKnownWeapons(Player p)
        {
            if (KnownWeapons.ContainsKey(p))
                return (int)(KnownWeapons[p] * _doppleMult);
            else
                return 0;
        }

        //return misleading information to other players
        public override string EngineMark
        {
            get
            {
                if (OwnedByCurrentPlayer)
                    return base.EngineMark;
                else
                    return "E" + DoppleEngines;
            }
        }
        public override string PrimarySystemMark
        {
            get
            {
                if (OwnedByCurrentPlayer)
                    return base.PrimarySystemMark;
                else if (DiscoveredByCurrentPlayer)
                    return "W" + DoppleValue;
                else if (KnownWeapons[Game.CurrentPlayer] > 0)
                    return "W" + GetDoppleValue(KnownWeapons[Game.CurrentPlayer]) + "?";
                else
                    return "";
            }
        }
        public override string EngineStatLabel
        {
            get
            {
                if (OwnedByCurrentPlayer)
                    return base.EngineStatLabel;
                else
                {
                    if (DoppleEngines < DoppleMaxEngines)
                        return DoppleEngines + "/" + DoppleMaxEngines;
                    else
                        return DoppleEngines.ToString();
                }
            }
        }
        public override string PrimarySystemStatLabel
        {
            get
            {
                if (OwnedByCurrentPlayer)
                    return base.PrimarySystemStatLabel;
                else
                {
                    if (DoppleValue < DoppleMaxValue)
                        return DoppleValue + "/" + DoppleMaxValue;
                    else
                        return DoppleValue.ToString();
                }
            }
        }

        //private members
        private double _doppleMult;

        //return the "dopple" value of a value
        private int GetDoppleValue(int value)
        {
            int val = (int)(value * _doppleMult);
            if (val < 1)
                val = 1;
            return val;
        }

        //persistence
        protected override string SubClassPersist()
        {
            Tokenizer tok = new Tokenizer();
            tok.Tokenize(_doppleMult);
            return base.SubClassPersist() + tok.Result;
        }
        protected override void SubClassParse(SVTokenizer tok)
        {
            base.SubClassParse(tok);
            _doppleMult = tok.TokenDouble;
        }
    }
}
